package at.fhooe.engine.time;

import java.util.HashMap;

public class NanoClock {
	public static long SECOND_IN_NANOS = 1000000000l;
	public static double ONE_NANOSECOND =  0.000000001;
	private long m_currentTime;
	private long m_lastTime;
	private long m_deltaTime;
	private HashMap<String, PandoraTimer> m_timers;
	
	public NanoClock() {
		m_currentTime = System.nanoTime();
		m_lastTime = System.nanoTime();
		m_timers = new HashMap<String, PandoraTimer>();
	}
	
	public void tick() {
		m_lastTime = m_currentTime;
		m_currentTime = System.nanoTime();
		m_deltaTime = m_currentTime - m_lastTime;
		
		for(PandoraTimer timer: m_timers.values()) {
			timer.update(getDeltaTime());
		}
	}
	
	public double timeSinceLastTick() {
		return (System.nanoTime() - m_currentTime) * ONE_NANOSECOND;
	}
	
	public double getDeltaTime() {
		return m_deltaTime * ONE_NANOSECOND;
	}
	
	public PandoraTimer createTimer(String name) {
		PandoraTimer pandoraTimer = null;
		if(!m_timers.containsKey(name)) {
			pandoraTimer = new PandoraTimer(name);
			m_timers.put(name, pandoraTimer);
		}
		return pandoraTimer;
	}

	public PandoraTimer getTimerByName(String name) {
		return m_timers.get(name);
	}
}
